package com.tao.com.xiaowen.module.me;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.tao.com.xiaowen.event.UserAuthEvent;
import com.tao.com.xiaowen.R;

import org.greenrobot.eventbus.EventBus;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by tao on 17-2-18.
 */

public class RegisterActivity extends AppCompatActivity {
    private EditText userEmail;
    private EditText userPassword;
    private EditText userName;
    private Button userRegister;
    private ImageButton btnReturn;
    private TextView titleView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
    }

    private void initView() {
        userEmail = (EditText) findViewById(R.id.user_email);
        userPassword = (EditText) findViewById(R.id.user_password);
        userName = (EditText) findViewById(R.id.user_name);
        userRegister = (Button) findViewById(R.id.user_register);
        btnReturn = (ImageButton) findViewById(R.id.return_button);
        titleView = (TextView) findViewById(R.id.title);
        titleView.setText("注册");

        btnReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        userRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String email = String.valueOf(userEmail.getText());
                final String password = String.valueOf(userPassword.getText());
                final String name = String.valueOf(userName.getText());

                BmobUser bmobUser = new BmobUser();
                bmobUser.setEmail(email);
                bmobUser.setPassword(password);
                bmobUser.setUsername(name);
                bmobUser.signUp(new SaveListener<Object>() {
                    @Override
                    public void done(Object o, BmobException e) {
                        if (e == null) {
                            SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                            editor.putString("email", email);
                            editor.putString("password", password);
                            editor.putString("name", name);
                            editor.commit();

                            Toast.makeText(RegisterActivity.this, "注册成功，请到" + email + "邮箱中进行激活。", Toast.LENGTH_SHORT).show();
                            EventBus.getDefault().post(new UserAuthEvent());
                            finish();
                        } else {
                            Toast.makeText(RegisterActivity.this, "失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });


            }
        });
    }
}
